package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.adobe.cairngorm.validation.validators.URLValidator;
	import com.housediyadmin.events.houseEvents.AddHouseEvent;
	import com.housediyadmin.events.houseEvents.EditHouseEvent;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.vo.House;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class HousePopUpMediator extends Mediator
	{
		[Inject]
		public var view:HousePopUp;
		[Inject]
		public var buildingModel:BuildingModel;
		
		[Inject]
		public var houseModel:HouseModel;
		
		public var smallFile:FileReference;
		public var bigFile:FileReference;
		
		public var request1:URLRequest;
		public var request2:URLRequest;
		[Bindable]
		public var smallFileName:String;
		
		[Bindable]
		public var bigFileName:String;
		private var newHouse:House;
		private var oldHouse:House;
		
		override public function onRegister():void
		{						
			//request = new URLRequest("http://localhost/housediyadmin/HouseUploadService.aspx");
			//上传小户型图
			smallFile = new FileReference();
			smallFile.addEventListener(Event.SELECT, selectSmallFileHandler);
			smallFile.addEventListener(ProgressEvent.PROGRESS, uploadSmallProgressHandler);
			view.browseSmallBtn.addEventListener(MouseEvent.CLICK, browseSmallHandler);
			
			//上传大户型图
			bigFile = new FileReference();
			bigFile.addEventListener(Event.SELECT, selectBigFileHandler);
			bigFile.addEventListener(ProgressEvent.PROGRESS, uploadBigProgressHandler);
			view.browseBigBtn.addEventListener(MouseEvent.CLICK, browseBigHandler);
						
			var dateFormatter:DateFormatter = new DateFormatter();    
			dateFormatter.formatString = "YYYY年MM月DD日";   
			view.houseStartTime.addEventListener(CalendarLayoutChangeEvent.CHANGE,getStartTime);
			newHouse=new House();
			
			
			if(view.type=="ADD")
			{
				BindingUtils.bindProperty(view.buildingName, "text", buildingModel.selectedBuilding, "buildingName");
				view.houseCreateDate.text= dateFormatter.format(new Date());
				view.houseEditDate.text= dateFormatter.format(new Date());
				view.houseStartTime.text=dateFormatter.format(null);
			}
			else if(view.type=="EDIT")
			{
				oldHouse=houseModel.selectedHouse;
				BindingUtils.bindProperty(view.buildingName, "text", houseModel.selectedHouse, "buildingID");
				BindingUtils.bindProperty(view.houseHoldName, "text", houseModel.selectedHouse, "houseHoldName");
				BindingUtils.bindProperty(view.houseHoldNum, "text", houseModel.selectedHouse, "houseHoldNum");
				BindingUtils.bindProperty(view.houseBuildingArea, "text", houseModel.selectedHouse, "houseBuildingArea");
				BindingUtils.bindProperty(view.houseInsideArea, "text", houseModel.selectedHouse, "houseInsideArea");
				BindingUtils.bindProperty(view.housePrice, "text", houseModel.selectedHouse, "housePrice");
				BindingUtils.bindProperty(view.housePicSrcSmall, "text", houseModel.selectedHouse, "housePicSrcSmall");
				BindingUtils.bindProperty(view.housePicSrcBig, "text", houseModel.selectedHouse, "housePicSrcBig");
				BindingUtils.bindProperty(view.houseRemark, "text", houseModel.selectedHouse, "houseRemark");
				BindingUtils.bindProperty(view.houseCreator, "text", houseModel.selectedHouse, "houseCreator");
				BindingUtils.bindProperty(view.houseEditor, "text", houseModel.selectedHouse, "houseEditor");
				
				var createDate:Date=houseModel.selectedHouse.houseCreateDate;
				var modifyDate:Date=new Date();
				var tempDate:Date=houseModel.selectedHouse.houseStartTime;
				tempDate.hours+=8;
				var startTime:Date=tempDate;
				newHouse.houseStartTime=oldHouse.houseStartTime;
				
				view.houseCreateDate.text=dateFormatter.format(createDate);
				view.houseEditDate.text=dateFormatter.format(modifyDate);
				view.houseStartTime.text=dateFormatter.format(startTime);
				
				view.houseStartTime.addEventListener(CalendarLayoutChangeEvent.CHANGE,getStartTime);
				
			}
			
			view.okBtn.addEventListener(MouseEvent.CLICK,okClk);
			view.cancelBtn.addEventListener(MouseEvent.CLICK,close);
			view.addEventListener(CloseEvent.CLOSE,close);
		}

		public function browseSmallHandler(event:MouseEvent):void
		{
			smallFile.browse(getTypes());
		}
		
		public function browseBigHandler(event:MouseEvent):void
		{
			bigFile.browse(getTypes());
		}
		
		private function getTypes():Array
		{
			var allTypes:Array = new Array(getImageTypeFilter());
			return allTypes;
		}
		
		private function getImageTypeFilter():FileFilter
		{
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}
		
		public function selectSmallFileHandler(event:Event):void
		{
			request1 = new URLRequest("http://10.60.1.120/HouseDiyAdmin/HouseUploadService.aspx");
			view.uploadSmallBar.visible = true;
			smallFile = FileReference(event.target);
			var variables:URLVariables = new URLVariables();
			variables.fileName  = "caddy" + "_" + new Date().time + smallFile.type;
			smallFileName = "caddy" + "_" + new Date().time + smallFile.type;	
			request1.data = variables;
			smallFile.upload(request1);
			view.housePicSrcSmall.text = smallFileName;			
		}		
		
		public function selectBigFileHandler(event:Event):void
		{
			request2 = new URLRequest("http://10.60.1.120/HouseDiyAdmin/HouseUploadService.aspx");
			view.uploadBigBar.visible = true;
			smallFile = FileReference(event.target);
			var variables:URLVariables = new URLVariables();
			variables.fileName  = "caddy" + "_" + new Date().time + smallFile.type;
			bigFileName = "caddy" + "_" + new Date().time + bigFile.type;	
			request2.data = variables;
			bigFile.upload(request2);
			view.housePicSrcSmall.text = bigFileName;	
		}
		
		public function uploadSmallProgressHandler(event:ProgressEvent):void
		{
			view.uploadSmallBar.setProgress(event.bytesLoaded/event.bytesTotal*100, 100) + "%";
			view.uploadSmallBar.label = "当前进度" + int(event.bytesLoaded/event.bytesTotal*100) + "%";
		}
		
		public function uploadBigProgressHandler(event:ProgressEvent):void
		{
			view.uploadBigBar.setProgress(event.bytesLoaded/event.bytesTotal*100, 100) + "%";
			view.uploadBigBar.label = "当前进度" + int(event.bytesLoaded/event.bytesTotal*100) + "%";
		}
		private function okClk(e:MouseEvent):void
		{
			newHouse.houseHoldName=view.houseHoldName.text;
			newHouse.houseHoldNum=int(view.houseHoldNum.text);
			newHouse.houseBuildingArea=int(view.houseBuildingArea.text);
			newHouse.houseInsideArea=int(view.houseInsideArea.text);
			newHouse.housePrice=Number(view.housePrice.text);
			newHouse.housePicSrcSmall=view.housePicSrcSmall.text;
			newHouse.housePicSrcBig=view.housePicSrcBig.text;
			newHouse.houseRemark=view.houseRemark.text;
			newHouse.houseCreator=view.houseCreator.text;
			newHouse.houseEditor=view.houseEditor.text;			
			if(view.type=="ADD")
			{
				newHouse.buildingID=buildingModel.selectedBuilding.buildingID;
				newHouse.houseCreateDate=new Date();
				newHouse.houseEditDate=new Date();
				
				if(errorInfor(newHouse)=="")
				{
					dispatch(new AddHouseEvent(AddHouseEvent.ADD_HOUSE,newHouse));
					Alert.show("添加成功！");
					close(e);
				}
				else
				{
					Alert.show(errorInfor(newHouse))
				}
			}
			else if(view.type=="EDIT")
			{
				newHouse.houseID=oldHouse.houseID;
				newHouse.buildingID=oldHouse.buildingID;
				newHouse.houseCreateDate=oldHouse.houseCreateDate;
				newHouse.houseEditDate=new Date();
				//houseStartTime在事件中给出
				
				dispatch(new EditHouseEvent(EditHouseEvent.EDIT_HOUSE,newHouse));
				Alert.show("修改成功！");
				close(e);
			}
				
			
		}
		
		private function 	errorInfor(checkHouse:House):String
		{
			var returnStr:String;
			if(!checkHouse.houseStartTime)
			{
				returnStr="请输入开盘时间！";
			}
			else
			{
				returnStr="";
			}
			return returnStr;
		}
			
		private function getStartTime(e:CalendarLayoutChangeEvent):void
		{
			if(DateField(e.target).selectedDate!=null)
			{
				var dateFormatter:DateFormatter = new DateFormatter();    
				dateFormatter.formatString = "YYYY年MM月DD日";   
				
				var selectedDate:Date=e.target.selectedDate;
//				selectedDate.hours++;
				view.houseStartTime.text=dateFormatter.format(selectedDate);
				newHouse.houseStartTime=selectedDate;
			}
		}
	
		private function close(e:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
	}
}